import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state, n = 1) {
      state.count += n;
    },
    decrement(state, n = 1) {
      state.count -= n;
    }
  },
  actions: {
    incrementIfOdd(context) {
      if ((context.state.count + 1) % 2 === 0) {
        context.commit('increment')
      }
    },
    incrementAsync(context) {
      // console.log(context);
      setTimeout(() => {
        context.commit('increment')
      }, 3000)
    }
  },
  getters: {
    total(state) {
      return state.count;
    }
  }
})

export default store
